<?php

namespace common\models;

use common\extended\system\Util;
use Yii;

/**
 * This is the model class for table "bus_service".
 *
 * @property string $service_id
 * @property string $service_member_name
 * @property string $service_member_id
 * @property string $service_member_phone
 * @property string $service_member_type
 * @property integer $service_status
 * @property integer $room_id
 * @property string $room_name
 * @property string $service_remark
 * @property string $service_addtime
 * @property string $make_time
 * @property string $shop_id
 * @property string $boss_id
 */
class BusService extends \yii\db\ActiveRecord
{
    public $errorMessage;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bus_service';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['service_member_name', 'service_member_id', 'service_member_phone', 'service_member_type', 'room_id', 'shop_id', 'boss_id'], 'required'],
            [['service_member_id', 'service_status', 'room_id', 'shop_id', 'boss_id'], 'integer'],
            [['service_addtime', 'make_time'], 'safe'],
            [['service_member_name', 'service_member_type', 'room_name'], 'string', 'max' => 45],
            [['service_member_phone'], 'string', 'max' => 11],
            [['service_remark'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'service_id' => 'Service ID',
            'service_member_name' => 'Service Member Name',
            'service_member_id' => 'Service Member ID',
            'service_member_phone' => 'Service Member Phone',
            'service_member_type' => 'Service Member Type',
            'service_status' => 'Service Status',
            'room_id' => '房间ID',
            'room_name' => '房间名称',
            'service_remark' => 'Service Remark',
            'service_addtime' => 'Service Addtime',
            'make_time' => '预约时间',
            'shop_id' => 'Shop ID',
            'boss_id' => 'Boss ID',
        ];
    }

    public function getExtends()
    {
        return $this->hasMany(BusServiceExtend::className(), ['service_id' => 'service_id']);
    }

    public function getMembers()
    {
        return $this->hasOne(BusMember::className(), ['member_id' => 'service_member_id']);
    }

    /**
     * @return boolean
     */
    public function checkParams()
    {
        if (!is_numeric($this->service_member_id) || $this->service_member_id < 1) {
            $this->errorMessage = '会员id错误';
            return false;
        }
        /*if (!is_numeric($this->service_beautycartd_id) || $this->service_beautycartd_id < 1) {
            $this->errorMessage = '消费卡id错误';
            return false;
        }*/
        /*if (!is_numeric($this->employee_id) || $this->employee_id < 1) {
            $this->errorMessage = '员工id错误';
            return false;
        }*/
        if ($this->service_remark && Util::utf8_strlen($this->service_remark) > 100) {
            $this->errorMessage = '备注不能超过100个字符';
            return false;
        }

        return true;
    }

    /**
     * 登记服务
     * @param bool $is_check 是否检查参数
     * @return bool
     */
    public function addData($is_check = true)
    {
        if ($is_check && !$this->checkParams()) {
            return false;
        }

        $memberInfo = BusService::findOne(['shop_id' => $this->shop_id, 'member_id' => $this->service_member_id])->toArray();
        $room = BusConfig::findOne(['shop_id' => $this->shop_id, 'config_id' => $this->room_id])->toArray();

        $this->setAttributes([
            'service_member_name' => $memberInfo['member_name'],
            'service_member_phone' => $memberInfo['member_mobile'],
            'service_member_type' => $memberInfo['member_grade'],
            'room_name' => $room['config_val'],
            'service_addtime' => date('Y-m-d H:i:s')
        ]);

        if ($this->insert(false)) {
            return true;
        } else {
            $this->errorMessage = '登记服务失败';
            return false;
        }
    }

}
